
/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.model.femModel.problemDriver;

import java.util.ListIterator;

import br.ufmg.dees.insane.analysisModel.AnalysisModel;
import br.ufmg.dees.insane.materialMedia.degeneration.Degeneration;
import br.ufmg.dees.insane.model.femModel.element.Element;
import br.ufmg.dees.insane.model.femModel.element.ParametricElement;
import br.ufmg.dees.insane.shape.Shape;
import br.ufmg.dees.insane.util.IMatrix;
import br.ufmg.dees.insane.util.IVector;

/** 
 * A class defining a fisically non-linear solid mech problem driver
 *
 * @author Camara, Luciana & Pitangueira, Roque
 * @since May 2005
 */
public class ParametricPhysicallyNonLinearSolidMech extends PhysicallyNonLinearSolidMech {

	private static final long serialVersionUID = 1L;

/* (non-Javadoc)
 * @see br.ufmg.dees.insane.model.femModel.problemDriver.ProblemDriver#getC(br.ufmg.dees.insane.model.femModel.element.Element)
 */
public IMatrix getC(Element e) {
    int numberOfDegreesOfFreedom = e.getNumberOfDegreesOfFreedom();
	IMatrix stiffnessMatrix = new IMatrix(numberOfDegreesOfFreedom,numberOfDegreesOfFreedom);
    stiffnessMatrix.setZero();
	IMatrix finalStiffnessMatrix = new IMatrix(numberOfDegreesOfFreedom,numberOfDegreesOfFreedom);
    finalStiffnessMatrix.setZero();
	ListIterator gns = ((ParametricElement)e).getDegenerations().listIterator();
    AnalysisModel analysisModel = e.getAnalysisModel();
    Shape shape = e.getShape();    
    IMatrix cartesianNodalCoordsMatrix = e.getCartesianNodalCoordsMatrix();
	while(gns.hasNext()) {
	    Degeneration degeneration = (Degeneration) gns.next();
        double[] naturalCoords = degeneration.getRepresentation().getNaturalcoords().toDouble();
        IVector shapeFunction = shape.getShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
        IMatrix derivedShapeFunction = shape.getDerivedShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
        IMatrix secondDerivedShapeFunction = shape.getSecondDerivedShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
		IMatrix matrixB = analysisModel.getInternalVariableOperator(derivedShapeFunction, secondDerivedShapeFunction, shapeFunction, cartesianNodalCoordsMatrix);
		matrixB = degeneration.internalVariableOperatorModifier(matrixB);
		IMatrix matrixC = degeneration.mountC();
		IMatrix k1= new IMatrix(numberOfDegreesOfFreedom, matrixC.getNumCol());
		k1.mulTransposeLeft(matrixB, matrixC);
		stiffnessMatrix.mul(k1,matrixB);
		double weight = degeneration.getRepresentation().getWeight();
		double jac = analysisModel.getJacobianTransformation(derivedShapeFunction,cartesianNodalCoordsMatrix);
		double coeff = e.getElmCoefficient();
		stiffnessMatrix.setScale(weight*jac*coeff);
		IMatrix matrixT = analysisModel.getTransformationMatrix(derivedShapeFunction,cartesianNodalCoordsMatrix);
		stiffnessMatrix.mul(matrixT);
		matrixT.transpose();
		matrixT.mul(stiffnessMatrix);
		finalStiffnessMatrix.add(matrixT);
	}
return(finalStiffnessMatrix);
}


/* (non-Javadoc)
 * @see br.ufmg.dees.insane.model.femModel.problemDriver.ProblemDriver#getR(br.ufmg.dees.insane.model.femModel.element.Element)
 */
public IVector getE(Element e) {
    IVector E = new IVector(e.getNumberOfDegreesOfFreedom());
    return E;
    
}

/* (non-Javadoc)
 * @see br.ufmg.dees.insane.model.femModel.problemDriver.ProblemDriver#getF(br.ufmg.dees.insane.model.femModel.element.Element)
 */
public IVector getF(Element e) {
    int numberOfDegreesOfFreedom = e.getNumberOfDegreesOfFreedom();
	IVector internalForce = new IVector(numberOfDegreesOfFreedom);
	internalForce.zero();
	ListIterator gns = ((ParametricElement)e).getDegenerations().listIterator();
    AnalysisModel analysisModel = e.getAnalysisModel();
    Shape shape = e.getShape();
    IMatrix cartesianNodalCoordsMatrix = e.getCartesianNodalCoordsMatrix();
	while(gns.hasNext()) {
        Degeneration degeneration = (Degeneration) gns.next();
        double[] naturalCoords = degeneration.getRepresentation().getNaturalcoords().toDouble();
        IVector shapeFunction = shape.getShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
        IMatrix derivedShapeFunction = shape.getDerivedShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
        IMatrix secondDerivedShapeFunction = shape.getSecondDerivedShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
		IVector f = new IVector(numberOfDegreesOfFreedom);
		double intFactor = analysisModel.getIntegrationFactor(shapeFunction, cartesianNodalCoordsMatrix,degeneration.getGeometricProperties());
		IMatrix matrixB = analysisModel.getInternalVariableOperator(derivedShapeFunction, secondDerivedShapeFunction,shapeFunction, cartesianNodalCoordsMatrix);
		matrixB = degeneration.internalVariableOperatorModifier(matrixB);
		matrixB.transpose();
        f.mul(matrixB,((ParametricElement)e).getDegenerationDualInternalVariables(degeneration));
        double jac = analysisModel.getJacobianTransformation(derivedShapeFunction,cartesianNodalCoordsMatrix);
        double weight = degeneration.getRepresentation().getWeight();
		double integrationCoef = (weight*jac*intFactor*(e.getElmCoefficient()));
		f.scale(integrationCoef);
		IMatrix matrixT = analysisModel.getTransformationMatrix(derivedShapeFunction,cartesianNodalCoordsMatrix);
		matrixT.transpose();
		internalForce.add(matrixT.mul(f));
	}
	return internalForce;	
}

/* (non-Javadoc)
 * @see br.ufmg.dees.insane.model.femModel.problemDriver.ProblemDriver#getIncrementalC(br.ufmg.dees.insane.model.femModel.element.Element)
 */
public IMatrix getIncrementalC(Element e) {
    int numberOfDegreesOfFreedom = e.getNumberOfDegreesOfFreedom();
	IMatrix stiffnessMatrix = new IMatrix(numberOfDegreesOfFreedom,numberOfDegreesOfFreedom);
	stiffnessMatrix.setZero();
    IMatrix finalStiffnessMatrix = new IMatrix(numberOfDegreesOfFreedom,numberOfDegreesOfFreedom);
    finalStiffnessMatrix.setZero();
	ListIterator gns = ((ParametricElement)e).getDegenerations().listIterator();
	AnalysisModel analysisModel = e.getAnalysisModel();
    Shape shape = e.getShape();
    IMatrix cartesianNodalCoordsMatrix = e.getCartesianNodalCoordsMatrix();
	while(gns.hasNext()) {
	    Degeneration degeneration = (Degeneration) gns.next();
        double[] naturalCoords = degeneration.getRepresentation().getNaturalcoords().toDouble();
        IVector shapeFunction = shape.getShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
        IMatrix derivedShapeFunction = shape.getDerivedShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
        IMatrix secondDerivedShapeFunction = shape.getSecondDerivedShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
		IMatrix matrixB = analysisModel.getInternalVariableOperator(derivedShapeFunction, secondDerivedShapeFunction,shapeFunction, cartesianNodalCoordsMatrix);
		matrixB = degeneration.internalVariableOperatorModifier(matrixB);
		IMatrix matrixC = degeneration.mountCt();
		IMatrix k1= new IMatrix(numberOfDegreesOfFreedom, matrixC.getNumCol());
		k1.mulTransposeLeft(matrixB, matrixC);
		stiffnessMatrix.mul(k1,matrixB);
		double intFactor = analysisModel.getIntegrationFactor(shapeFunction, cartesianNodalCoordsMatrix,degeneration.getGeometricProperties());
		double weight = degeneration.getRepresentation().getWeight();
		double jac = analysisModel.getJacobianTransformation(derivedShapeFunction,cartesianNodalCoordsMatrix);
		double coeff = e.getElmCoefficient();
		stiffnessMatrix.setScale(weight*jac*intFactor*coeff);
		IMatrix matrixT = analysisModel.getTransformationMatrix(derivedShapeFunction,cartesianNodalCoordsMatrix);
		stiffnessMatrix.mul(matrixT);
		matrixT.transpose();
		matrixT.mul(stiffnessMatrix);
		finalStiffnessMatrix.add(matrixT);
	}
return(finalStiffnessMatrix);
}

/* (non-Javadoc)
 * @see br.ufmg.dees.insane.model.femModel.problemDriver.ProblemDriver#getTotalC(br.ufmg.dees.insane.model.femModel.element.Element)
 */
public IMatrix getTotalC(Element e) {
    int numberOfDegreesOfFreedom = e.getNumberOfDegreesOfFreedom();
	IMatrix stiffnessMatrix = new IMatrix(numberOfDegreesOfFreedom,numberOfDegreesOfFreedom);
	stiffnessMatrix.setZero();
    IMatrix finalStiffnessMatrix = new IMatrix(numberOfDegreesOfFreedom,numberOfDegreesOfFreedom);
    finalStiffnessMatrix.setZero();
	ListIterator gns = ((ParametricElement)e).getDegenerations().listIterator();
	AnalysisModel analysisModel = e.getAnalysisModel();
    Shape shape = e.getShape();
    IMatrix cartesianNodalCoordsMatrix = e.getCartesianNodalCoordsMatrix();
	while(gns.hasNext()) {
	    Degeneration degeneration = (Degeneration) gns.next();
        double[] naturalCoords = degeneration.getRepresentation().getNaturalcoords().toDouble();
        IVector shapeFunction = shape.getShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
        IMatrix derivedShapeFunction = shape.getDerivedShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
        IMatrix secondDerivedShapeFunction = shape.getSecondDerivedShapeFunction(naturalCoords, cartesianNodalCoordsMatrix);
		IMatrix matrixB = analysisModel.getInternalVariableOperator(derivedShapeFunction, secondDerivedShapeFunction,shapeFunction, cartesianNodalCoordsMatrix);
		matrixB = degeneration.internalVariableOperatorModifier(matrixB);
		IMatrix matrixC = degeneration.mountCs();
		IMatrix k1= new IMatrix(numberOfDegreesOfFreedom, matrixC.getNumCol());
		k1.mulTransposeLeft(matrixB, matrixC);
		stiffnessMatrix.mul(k1,matrixB);
		double intFactor = analysisModel.getIntegrationFactor(shapeFunction, cartesianNodalCoordsMatrix,degeneration.getGeometricProperties());
		double weight = degeneration.getRepresentation().getWeight();
		double jac = analysisModel.getJacobianTransformation(derivedShapeFunction,cartesianNodalCoordsMatrix);
		double coeff = e.getElmCoefficient();
		stiffnessMatrix.setScale(weight*jac*intFactor*coeff);
		IMatrix matrixT = analysisModel.getTransformationMatrix(derivedShapeFunction,cartesianNodalCoordsMatrix);
		stiffnessMatrix.mul(matrixT);
		matrixT.transpose();
		matrixT.mul(stiffnessMatrix);
		finalStiffnessMatrix.add(matrixT);
	}
return(finalStiffnessMatrix);
}

}
